package com.xxx.yuedong.campaign.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.xxx.yuedong.campaign.controller.form.AddTeamByIdForm;
import com.xxx.yuedong.campaign.controller.form.InsertTeamForm;
import com.xxx.yuedong.campaign.pojo.Team;
import com.xxx.yuedong.campaign.service.TeamService;
import com.xxx.yuedong.common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * @author HG
 * @version 1.0
 **/
@Api(tags = "跑团相关接口")
@RestController
@RequestMapping("/team")
@Slf4j
public class TeamController {
    @Autowired
    private TeamService teamService;

    @PostMapping("/insert")
    @ApiOperation("发布跑团")
    public R insert(@Valid @RequestBody InsertTeamForm form) {
        Team team = JSONUtil.parse(form).toBean(Team.class);
        team.setUserId(StpUtil.getLoginIdAsLong());
        ArrayList<Long> members = new ArrayList<>();
        members.add(StpUtil.getLoginIdAsLong());
        team.setMembers(members.toString());
        team.setStatus(1);
        team.setCreateTime(new Date());

        int rows = teamService.insert(team);

        return R.ok().put("rows", rows);
    }

    @PostMapping("/addTeamById")
    @ApiOperation("加入跑团")
    public R addTeamById(@Valid @RequestBody AddTeamByIdForm form) {
        int rows = teamService.addTeamById(form.getId());

        return R.ok().put("rows", rows);
    }

    @PostMapping("/searchAllTeam")
    @ApiOperation("查询所有跑团")
    public R addTeamById() {
        ArrayList<HashMap> teamList = teamService.searchAllTeam();

        return R.ok().put("teamList", teamList);
    }
}
